package com.demo.apparel.common;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "统一响应结果封装类")
public class Result<T> {

    @Schema(description = "响应状态码，200表示成功，其他表示失败")
    private String code;

    @Schema(description = "响应消息")
    private String msg;

    @Schema(description = "响应数据")
    private T data;

    public Result() {}

    public Result(T data) {
        this.data = data;
    }

    // 成功且带数据
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>(data);
        result.setCode("200");
        result.setMsg("成功");
        return result;
    }

    //成功带两个参数
    public static <T> Result<T> successes(T data, String msg) {
        Result<T> r = new Result<>();
        r.setCode("200");
        r.setData(data);
        r.setMsg(msg);
        return r;
    }



    // 仅成功，不带数据
    public static Result<Void> success() {
        return success(null);
    }

    // 自定义错误码和消息
    public static Result<?> error(String code, String msg) {
        Result<?> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    // 简单失败，code默认500
    public static Result<?> failure(String msg) {
        return error("500", msg);
    }

    // 方便直接失败
    public static Result<?> fail(String msg) {
        return failure(msg);
    }

    // --- getter/setter ---
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
